// pages/hot-new/hot-new.js
var app = getApp();
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    x: 0,
    newsList: [],
    brand_id: 59311, // 当前选中的id值https://blog.csdn.net/brokenkay/article/details/96871720
    paixu: "viewDown",//按浏览量排序newer
    newsArt: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var newsListUrl = app.globalData.it120Base + "/liujiahe/cms/category/list";
    util.gethttp(newsListUrl, this.processNewsList);
    var newsArtUrl = app.globalData.it120Base + "/liujiahe/cms/news/list/v2?orderBy=" + this.data.paixu + "&categoryId=" + this.data.brand_id;
    util.posthttp(newsArtUrl, this.processNewsArt);
  },
  processNewsList: function (data) {
    this.setData({ newsList: data.data });
  },
  processNewsArt: function (data) {
    console.log(data);
    var newsArt = [];
    console.log(data);
    for (var idx in data.data.result) {
      //数据处理 时间 题目  关键词2个 浏览量 缩略图
      var news = data.data.result[idx];
      console.log(news);
      var title = news.title;
      if (title.length >= 15) {
        title = title.substring(0, 15) + "...";
      }
      var key = [];
      key.push(news.keywords.split(",")[0]); key.push(news.keywords.split(",")[1]);
      var time ;//时间处理
      var start_date = new Date(news.dateAdd.split(" ")[0].replace(/-/g, "/"));
      var end_date = new Date();
      var days = end_date.getTime() - start_date.getTime();
      var day = parseInt(days / (1000 * 60 * 60 * 24));
      if(day==0){
        time= "今天";
      }else if(day<7){
        time= day+"天前";
      }else {
        time= news.dateAdd.split(" ")[0];
      }
      var temp = {
        title: title,
        key: key,
        time: time,
        pic: news.pic,
        views: news.views,
        id: news.id,
      }
      newsArt.push(temp);
    }
    this.setData({ newsArt: newsArt });
    wx.hideNavigationBarLoading({
      complete: (res) => { },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  switchTap(e) { //更换资讯大类
    let screenWidth = wx.getSystemInfoSync().windowWidth;
    let itemWidth = screenWidth / 5;
    let { index, id } = e.currentTarget.dataset;
    const { newsList } = this.data;
    let scrollX = itemWidth * index - itemWidth * 2;
    let maxScrollX = (newsList.length + 1) * itemWidth;
    if (scrollX < 0) {
      scrollX = 0;
    } else if (scrollX >= maxScrollX) {
      scrollX = maxScrollX;
    }
    this.setData({
      x: scrollX,
      brand_id: id
    })
    var newsArtUrl = app.globalData.it120Base + "/liujiahe/cms/news/list/v2?orderBy=" + this.data.paixu + "&categoryId=" + this.data.brand_id;
    util.posthttp(newsArtUrl, this.processNewsArt);

    this.triggerEvent("switchTap", id); //点击了导航,通知父组件重新渲染列表数据
  },
  onNewlistToNewid: function (event) {
    var newId = event.currentTarget.dataset.newId;
    wx.setStorageSync('NewList', wx.getStorageSync('NewList')+","+newId);
    console.log(newId);
    wx.navigateTo({
      url: '../new-id/new-id?id=' + newId,
    })
  }
})